/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.extremecomponents.table.view;


import java.util.Iterator;

import org.extremecomponents.table.bean.Export;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.view.freemarker.extend.MessageExtend;
import org.extremecomponents.table.view.html.BuilderConstants;
import org.extremecomponents.table.view.html.BuilderUtils;
import org.extremecomponents.table.view.html.EccnToolbarBuilder;
//import org.extremecomponents.table.view.html.ToolbarBuilder;
import org.extremecomponents.table.view.html.TwoColumnRowLayout;
import org.extremecomponents.util.HtmlBuilder;
//import org.extremecomponents.table.limit.Limit;

/**
 * @author guanhw
 */
public class EccnToolbar extends TwoColumnRowLayout {

	public EccnToolbar ( HtmlBuilder html , TableModel model ) {
		super( html, model );
	}

	protected boolean showLayout ( TableModel model ) {
		boolean showStatusBar = BuilderUtils.showStatusBar( model );
		// boolean filterable = BuilderUtils.filterable(model);
		boolean showExports = BuilderUtils.showExports( model );
		boolean showPagination = BuilderUtils.showPagination( model );
		boolean showTitle = BuilderUtils.showTitle( model );
		if ( !showStatusBar && !showExports && !showPagination && !showTitle ) {
			return false;
		}

		return true;
	}

	protected void columnLeft ( HtmlBuilder html , TableModel model ) {
		boolean showExports = BuilderUtils.showExports( model );
		EccnToolbarBuilder toolbarBuilder = new EccnToolbarBuilder( html, model );
		html.td( 2 ).close();
		html.table( 2 ).border( "0" ).cellPadding( "0" ).cellSpacing( "1" ).styleClass( BuilderConstants.TOOLBAR_CSS ).close();
		html.tr( 3 ).close();
		if ( showExports ) {
			Iterator iterator = model.getExportHandler().getExports().iterator();
			for ( Iterator iter = iterator; iter.hasNext(); ) {
				html.td( 4 ).close();
				Export export = ( Export ) iter.next();
				toolbarBuilder.exportItemAsImage( export );
				html.tdEnd();
			}
		}
		html.td ( 4 ).close ( );
		toolbarBuilder.separator ( );
		html.tdEnd ( );
		boolean filterable = BuilderUtils.filterable ( model ) ; 
        if ( filterable ) {
            html.td(5).styleClass(BuilderConstants.FILTER_BUTTONS_CSS).close();
            
            /*
            html.img();
            html.src(BuilderUtils.getImage(model, BuilderConstants.TOOLBAR_FILTER_ARROW_IMAGE));
            html.style("border:0");
            html.alt("Arrow");
            html.xclose();
            */
               
            html.nbsp();

            toolbarBuilder.filterItemAsText();

            html.nbsp();

            toolbarBuilder.clearItemAsText();
    
            html.tdEnd();		
        }


		
		
		
		///
		html.trEnd( 3 );
		
		html.tableEnd( 2 );
		html.newline();
		html.tabs( 2 );

		html.tdEnd();

	}

	protected void columnRight ( HtmlBuilder html , TableModel model ) {
		/*
        int currentRowsDisplayed = getTableModel().getLimit().getCurrentRowsDisplayed();
        int currentPage = getTableModel().getLimit().getPage();
        int ys = 0;
		int totalPages = 0;
		int totalRows = getTableModel().getLimit().getTotalRows();
		if (currentRowsDisplayed > 0) {
			ys = totalRows % currentRowsDisplayed;
			totalPages = ys == 0 ? totalRows / currentRowsDisplayed : totalRows / currentRowsDisplayed + 1;
		} else {
			totalPages = 1;
			currentPage = 1;
		}
		*/
		boolean showPagination = BuilderUtils.showPagination( model );
		boolean showGotoPages = Boolean.valueOf( ( String ) model.getTableHandler().getTable().getAttribute( "showGotoPages" ) ).booleanValue();
		boolean showRowsDisplayed = Boolean.valueOf(
				( String ) model.getTableHandler().getTable().getAttribute(
						"showRowsDisplayed" ) ).booleanValue();
		EccnToolbarBuilder toolbarBuilder = new EccnToolbarBuilder( html, model );
		html.td( 2 ).align( "right" ).close();
		html.table( 2 ).border( "0" ).cellPadding( "0" ).cellSpacing( "1" )
				.styleClass( BuilderConstants.TOOLBAR_CSS ).close();
		html.tr( 3 ).close();

		
		
		MessageExtend message = new MessageExtend( model ) ;
		if ( showPagination ) {
			html.td( 4 ).close();
			html.append ( message.getResultsFound ( )  ) ;
			html.tdEnd();
			
			
			html.td( 4 ).close();
			toolbarBuilder.firstPageItemAsText ( ) ;//firstPageItemAsImage();
			html.tdEnd();

			html.td( 4 ).close();
			toolbarBuilder.prevPageItemAsText();
			html.tdEnd();

			html.td( 4 ).close();
			toolbarBuilder.nextPageItemAsText();
			html.tdEnd();

			html.td( 4 ).close();
			toolbarBuilder.lastPageItemAsText();
			html.tdEnd();

			html.td( 4 ).close();
			toolbarBuilder.separator();
			html.tdEnd();

		
			if ( showRowsDisplayed ) {
				html.td( 4 ).close();
				toolbarBuilder.rowsDisplayed();
				html.tdEnd();
			}

			if ( showGotoPages ) {
				html.td( 4 ).close();
				toolbarBuilder.gotoPagesDroplist();
				html.tdEnd();
			}

		}

		html.trEnd( 3 );
		html.tableEnd( 2 );
		html.newline();
		html.tabs( 2 );
		html.tdEnd();
	}
	
	protected void statusBar ( TableModel model ) {
		new DefaultStatusBar( getHtmlBuilder(), model ).layout();
	}
}
